package com.bing.services.impl;

import com.bing.Component.Context;
import com.bing.bean.weixin.MsgType;
import com.bing.bean.weixin.RequestMessage;
import com.bing.bean.weixin.ResponseMessage;
import com.bing.services.CommandService;
import com.bing.services.WeiXinService;
import com.bing.utils.MsgHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.OrderComparator;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kaibb on 2014/12/3.
 */
public abstract class AbstractWeiXinService implements WeiXinService {
    private final static Logger logger = LoggerFactory.getLogger(AbstractWeiXinService.class);

    @Resource(name = "commandServices")
    private List<CommandService> commandServices = new ArrayList<CommandService>();

    @Override
    public ResponseMessage handleCommand(Context context) {
        logger.info("开始处理command:{}", context.getCommand());
        ResponseMessage responseMessage = null;
        boolean skipContext = false;
        if ("!".equals(context.getCommand()) || "！".equals(context.getCommand())) {
            context.setCommand(context.getUser().getCommand() == null ? "" : context.getUser().getCommand());
            context.getRequestMessage().setContent(context.getUser().getHistoryCommands());
            skipContext = true;
        }
        //如果以@开头,则截取第一个空格前头字符做为command,后头字符做为content
        if (context.getRequestMessage().getMsgType()== MsgType.text&&context.getRequestMessage().getContent().startsWith("@")) {
            String command = context.getCommand() + " ";
            context.setCommand(command.substring(0,command.indexOf(" ")));
            context.getRequestMessage().setContent(command.substring(command.indexOf(" ")).trim());
            skipContext = true;
        }
        if (!skipContext && context.getRequestMessage().getMsgType()== MsgType.text && StringUtils.hasText(context.getUser().getCommand()) && (context.getUser().getLatestActive().getTime() + 10 * 60 * 1000 > System.currentTimeMillis())) {
            logger.info("当前context:{}", context.getUser().getCommand());
            context.setCommand(context.getUser().getCommand());
        }
        for (CommandService commandService : commandServices) {
            if (commandService.canHandle(context)) {
                responseMessage = commandService.handle(context);
                break;
            }
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage handleEvent(Context context) {
        logger.info("开始处理事件消息");
        ResponseMessage responseMessage = null;
        return responseMessage;
    }

    protected abstract String processText(RequestMessage requestMessage);


    @Override
    public ResponseMessage handleImage(Context context) {
        logger.info("开始处理图片消息  ");
        ResponseMessage imageMsg = MsgHelper.toImage(context.getRequestMessage());
        return imageMsg;
    }

    @Override
    public ResponseMessage handleVoice(Context context) {
        return null;
    }

    @Override
    public ResponseMessage handleVideo(Context context) {
        return null;
    }

    @Override
    public ResponseMessage handleLocation(Context context) {
        logger.info("开始处理地理位置消息");
        ResponseMessage textMsg = MsgHelper.toText(context.getRequestMessage(), context.getRequestMessage().getLabel());
        return textMsg;
    }

    @Override
    public ResponseMessage handleMews(Context context) {
        return null;
    }


    @PostConstruct
    public void init() {
        logger.info("command 排序");
        OrderComparator.sort(commandServices);
    }

}